/**
 * 证据实体类
 * 表示案件中的各种证据
 */
export enum EvidenceType {
  PHYSICAL = 'physical',      // 物证
  DOCUMENTARY = 'documentary', // 书证
  TESTIMONIAL = 'testimonial', // 证人证言
  DIGITAL = 'digital',        // 数字证据
  FORENSIC = 'forensic',      // 法医证据
  SURVEILLANCE = 'surveillance', // 监控证据
  AUDIO = 'audio',            // 音频证据
  PHOTOGRAPHIC = 'photographic' // 照片证据
}

export enum EvidenceStatus {
  COLLECTED = 'collected',    // 已收集
  ANALYZED = 'analyzed',      // 已分析
  ADMITTED = 'admitted',      // 已采纳
  REJECTED = 'rejected',      // 已拒绝
  PENDING = 'pending'         // 待处理
}

export enum EvidenceImportance {
  CRITICAL = 'critical',      // 关键证据
  IMPORTANT = 'important',    // 重要证据
  SUPPORTING = 'supporting',  // 辅助证据
  CIRCUMSTANTIAL = 'circumstantial' // 旁证
}

export interface Evidence {
  id: string;
  caseId: string;
  title: string;
  description: string;
  type: EvidenceType;
  status: EvidenceStatus;
  importance: EvidenceImportance;
  foundAt: string; // 发现时间
  foundBy: string; // 发现者
  location: string; // 发现地点
  imageUrl?: string;
  documentUrl?: string;
  metadata: Record<string, any>; // 额外元数据
  isKeyEvidence: boolean; // 是否关键证据
  analysisNotes: string[];
  chainOfCustody: string[]; // 保管链记录
  authenticity: number; // 真实性评分 0-100
  reliability: number; // 可靠性评分 0-100
  relevance: number; // 相关性评分 0-100
  tags: string[];
  relatedEvidenceIds: string[]; // 相关证据ID
  relatedSuspectIds: string[]; // 相关嫌疑人ID
}

/**
 * 创建新证据的工厂函数
 */
export function createEvidence(data: Partial<Evidence> = {}): Evidence {
  const now = new Date().toISOString();
  
  return {
    id: data.id || generateEvidenceId(),
    caseId: data.caseId || '',
    title: data.title || '未命名证据',
    description: data.description || '',
    type: data.type || EvidenceType.PHYSICAL,
    status: data.status || EvidenceStatus.COLLECTED,
    importance: data.importance || EvidenceImportance.SUPPORTING,
    foundAt: data.foundAt || now,
    foundBy: data.foundBy || '未知',
    location: data.location || '未知地点',
    imageUrl: data.imageUrl,
    documentUrl: data.documentUrl,
    metadata: data.metadata || {},
    isKeyEvidence: data.isKeyEvidence || false,
    analysisNotes: data.analysisNotes || [],
    chainOfCustody: data.chainOfCustody || [],
    authenticity: data.authenticity || 0,
    reliability: data.reliability || 0,
    relevance: data.relevance || 0,
    tags: data.tags || [],
    relatedEvidenceIds: data.relatedEvidenceIds || [],
    relatedSuspectIds: data.relatedSuspectIds || [],
    ...data
  };
}

/**
 * 生成证据ID
 */
function generateEvidenceId(): string {
  const timestamp = Date.now();
  const random = Math.random().toString(36).substr(2, 9);
  return `EVID-${timestamp}-${random}`;
}

/**
 * 获取证据类型的中文名称
 */
export function getEvidenceTypeName(type: EvidenceType): string {
  const typeNames = {
    [EvidenceType.PHYSICAL]: '物证',
    [EvidenceType.DOCUMENTARY]: '书证',
    [EvidenceType.TESTIMONIAL]: '证人证言',
    [EvidenceType.DIGITAL]: '数字证据',
    [EvidenceType.FORENSIC]: '法医证据',
    [EvidenceType.SURVEILLANCE]: '监控证据',
    [EvidenceType.AUDIO]: '音频证据',
    [EvidenceType.PHOTOGRAPHIC]: '照片证据'
  };
  return typeNames[type];
}

/**
 * 获取证据状态的中文名称
 */
export function getEvidenceStatusName(status: EvidenceStatus): string {
  const statusNames = {
    [EvidenceStatus.COLLECTED]: '已收集',
    [EvidenceStatus.ANALYZED]: '已分析',
    [EvidenceStatus.ADMITTED]: '已采纳',
    [EvidenceStatus.REJECTED]: '已拒绝',
    [EvidenceStatus.PENDING]: '待处理'
  };
  return statusNames[status];
}

/**
 * 获取证据重要性的中文名称
 */
export function getEvidenceImportanceName(importance: EvidenceImportance): string {
  const importanceNames = {
    [EvidenceImportance.CRITICAL]: '关键证据',
    [EvidenceImportance.IMPORTANT]: '重要证据',
    [EvidenceImportance.SUPPORTING]: '辅助证据',
    [EvidenceImportance.CIRCUMSTANTIAL]: '旁证'
  };
  return importanceNames[importance];
}

/**
 * 计算证据综合评分
 */
export function getEvidenceScore(evidence: Evidence): number {
  const { authenticity, reliability, relevance } = evidence;
  return Math.round((authenticity + reliability + relevance) / 3);
}

/**
 * 获取证据状态颜色
 */
export function getEvidenceStatusColor(status: EvidenceStatus): string {
  const statusColors = {
    [EvidenceStatus.COLLECTED]: '#3498db',
    [EvidenceStatus.ANALYZED]: '#2ecc71',
    [EvidenceStatus.ADMITTED]: '#27ae60',
    [EvidenceStatus.REJECTED]: '#e74c3c',
    [EvidenceStatus.PENDING]: '#f39c12'
  };
  return statusColors[status];
}

/**
 * 获取证据重要性颜色
 */
export function getEvidenceImportanceColor(importance: EvidenceImportance): string {
  const importanceColors = {
    [EvidenceImportance.CRITICAL]: '#c0392b',
    [EvidenceImportance.IMPORTANT]: '#e67e22',
    [EvidenceImportance.SUPPORTING]: '#f39c12',
    [EvidenceImportance.CIRCUMSTANTIAL]: '#95a5a6'
  };
  return importanceColors[importance];
}